UNIT Echo;

(* The following MPW commands will build the dcmd and copy it to the
   "Debugger Prefs" file in the System folder. The dcmd's name in
		 MacsBug will be the name of the file built by the Linker.

	    Pascal Echo.p
	    Link dcmdGlue.a.o Echo.p.o {Libraries}Runtime.o {PLibraries}PasLib.o -o Echo
	    BuildDcmd Echo 101
*)

{$R-}

INTERFACE

		USES MemTypes, dcmds;
		
  { Public declaration for dcmdGlue. Must be in every dcmd. The name cannot be changed. }
		PROCEDURE CommandEntry (paramPtr: dcmdBlockPtr);


IMPLEMENTATION

CONST CR = $0D;


PROCEDURE NumberToHex (number: LONGINT; VAR hex: Str255);
VAR digits: Str255;
    n: INTEGER;
BEGIN
  digits := '0123456789ABCDEF';
		hex    := '00000000';
		FOR n := 8 DOWNTO 1 DO
		  BEGIN
				hex[n] := digits[1 + (number MOD 16)];
				number := number DIV 16;
				END;
END;


PROCEDURE CommandEntry (paramPtr: DCmdBlockPtr);
VAR pos:   INTEGER;
    ch:    CHAR;
				value: LONGINT;
				ok:    BOOLEAN;
				str:   Str255;
BEGIN
  IF paramPtr^.request = dcmdInit THEN
		  BEGIN { The dcmd gets called once when loaded to init itself }
				END
		ELSE
  IF paramPtr^.request = dcmdDoIt THEN
		  BEGIN { Do the command's normal function }
				dcmdDrawLine ('Echoing parameters');

    REPEAT
      { Save the position so we can rewind if we get an error }
				  pos := dcmdGetPosition;
						ch  := dcmdPeekAtNextChar;
						IF ch IN ['A'..'Z', 'a'..'z'] THEN
								BEGIN { Get the parameter as a text string }
								ch := dcmdGetNextParameter (str);
								dcmdDrawLine (str);
								END
						ELSE
								BEGIN { Get the parameter as a 32 bit value }
								ch := dcmdGetNextExpression (value, ok);
								IF ok THEN
										BEGIN { The expression was parsed correctly }
										NumberToHex  (value, str);
										dcmdDrawLine (str);
										END
								ELSE
								  BEGIN { The expression contained an error. Get it as a string }
										dcmdSetPosition (pos);
								  ch := dcmdGetNextParameter (str);
								  dcmdDrawLine (str);
										END;
								END;
				UNTIL ch = CHR(CR);
				END
		ELSE
  IF paramPtr^.request = dcmdHelp THEN
		  BEGIN { Display the command's help information }
				dcmdDrawLine ('ECHO [params...]');
				dcmdDrawLine ('   Echo the command line parameters');
				END;
END;

END.
